import api from '@/api/batch'

const state = {
    rows: [],
    count: 0,
    data: {},
}

const actions = {
    //加载客户信息
    async getList({ commit, state }, listQuery = {}) {
        let where = {}
        if (listQuery.customer_id) {
            where.customer_id = listQuery.customer_id
        }
        if (listQuery.status_text) {
            where.status_text = listQuery.status_text
        }
        if (listQuery.time) {
            where.updated_at = {
                $gt: listQuery.time[0],
                $lt: listQuery.time[1],
            }
        }
        let response = await api.page({
            page: listQuery.page || 1,
            limit: listQuery.limit || 1000,
            where,
        })
        commit('updateState', response.data)
    },

    //保存批次产品信息
    async save() {

    },

    //发布报价
    async publish({ commit, state }, form) {
        let response = await api.publish(form)
        commit('updateState', { data: response.data })
    },

    //获取批次里的产品数据
    async getProduct({ commit, state }, listQuery = {}) {
        let response = await api.getBatchAndProduct(listQuery)
        commit('updateState', { data: response.data })
    }
}

const mutations = {
    updateState(state, stateData) {
        Object.assign(state, stateData)
    },
}


export default {
    namespaced: true,
    state,
    actions,
    mutations
}